import InteractiveBase from "../../../LevelCommon/InteractiveBase";
import LevelBaseControl from "../../../LevelCommon/LevelBaseControl";
import { GamePropData } from "../../../Data/GamePropData";
import GamePropConst from "../../../../constant/GamePropConst";
import CommonConstant from "../../../../constant/CommonConstant";
import SparkPlug from "./SparkPlug";
import { utils } from "../../../../utils/CommonUtil";

export default class SparkPlugBox extends InteractiveBase {
    /** 火花塞的位置*/
    private sparkPos: Laya.Sprite3D;
    /** 拔出火花塞后要关闭的门名字*/
    public GateName: string;

    private mowner: Laya.Sprite3D;

    private curSelectId: number;

    /** 需要的真正的道具id*/
    public needPropId: number;
    
    onStart() {
        this.mowner = this.owner as Laya.Sprite3D;
        this.sparkPos = this.mowner.getChildByName("sparkPos") as Laya.Sprite3D;
    }
  
    protected InteractiveLogic(): boolean {
        /** 如果当前有火花塞就直接返回*/
        if (this.sparkPos.numChildren != 0 && this.sparkPos.getChildAt(0).activeInHierarchy && 
         this.sparkPos.getChildAt(0).getChildAt(0).activeInHierarchy) {
            return false;
        }
        let curLevelControl: LevelBaseControl = window.gameFacade.LevelMng.GetCurLevelControl();
        let curSelectProp: GamePropData = curLevelControl.CurSelectProp;
        this.curSelectId = -1;
        if (curSelectProp != null) {
            this.curSelectId = curSelectProp.id;
        }
        /** 如果选择了火花塞*/
        if (this.curSelectId == GamePropConst.CIRCLE_SPARK_PLUG ||
            this.curSelectId == GamePropConst.CUBE_SPARK_PLUG ||
            this.curSelectId == GamePropConst.TRAPEZOID_SPARK_PLUG ||
            this.curSelectId == GamePropConst.DIAMOND_SPARK_PLUG) {
            curLevelControl.UseCurProp();
            /** 生成火花塞物体*/
            let sparkPlugPath = CommonConstant._sparkPlugs + this.curSelectId+".lh";
            window.gameFacade.ResourceMng.LoadSprite3D<Laya.Sprite3D>(
                sparkPlugPath,
                this.onSparkPlugLoadSucc, this);
        }
        return false;
    }

    private onSparkPlugLoadSucc(sparkObj: Laya.Sprite3D) {
        let targetSparkObj = sparkObj.getChildAt(0) as Laya.Sprite3D;
        targetSparkObj.active = true;
        this.sparkPos.addChild(sparkObj);
        let SparkPlug_targetSparkObj = utils.CommonUtils.CustomAddComponent(targetSparkObj,SparkPlug) as SparkPlug;
        SparkPlug_targetSparkObj.gamePropId = this.curSelectId;
        SparkPlug_targetSparkObj.curSparkBox = this;
        
        sparkObj.transform.localPosition = new Laya.Vector3(0, 0, 0);
        sparkObj.transform.localScale = new Laya.Vector3(1, 1, 1);

        if (this.curSelectId == this.needPropId) {
            console.log("有门打开了！！！");
            /** 开门*/
            this.CallToLevel(this.GateName + "_Spark_Open");
        }
        /** 重新设置子交互物体  因为新生成了东西*/
        if (this.parentInteractive != null) {
            this.parentInteractive.SetAllSonObj();
        }
    }
    /** 拔出了火花塞*/
    public PullOutSpark(id: number) {
        if (id == this.needPropId) {
            console.log("有门被关上了！！");
            /** 关对应的门*/
            this.CallToLevel(this.GateName + "_Spark_Close");
        }
    }
}